/**
 * Created by Yu Jin Xian on 2016/12/5.
 */
function dynamicProperties() {
    var person = new Object();
    person.name = 'Nicholas';
    alert(person.name);         // "Nicholas"

    function NotAddPropertiesPrimitiveTypes() {
        var person = 'Nicholas';
        person.age = 27;
        alert(person.age);      // undefined
    }
}

function cloneValueVariables() {
    var num1 = 5;
    var num2 = num1;

    function cloneReferenceVariables() {
        var obj1 = new Object();
        var obj2 = obj1;
        obj1.name = 'Nicholas';
        alert(obj2.name);       // "Nicholas"
    }
}

function functionArguments() {
    function addTen(num) {
        num += 10;
        return num;
    }

    var count = 20;
    var result = addTen(count);
    alert(count);               // 20, 没有变化
    alert(result);              // 30

    function objectArguments() {
        function setName(obj) {
            obj.name = 'Nicholas';
        }

        var person = new Object();
        setName(person);
        alert(person.name);     // "Nicholas"
    }

    function objectArguments2() {
        function setName(obj) {
            obj.name = 'Nicholas';
            obj = new Object();
            obj.name = 'Greg';
        }

        var person = new Object();
        setName(person);
        alert(person.name);     // "Nicholas"
    }

    function determiningType() {
        var s = 'Nicholas';
        var b = true;
        var i = 22;
        var u;
        var n = null;
        var o = new Object();

        alert(typeof s);        // string
        alert(typeof b);        // boolean
        alert(typeof i);        // number
        alert(typeof u);        // undefined
        alert(typeof n);        // object
        alert(typeof o);        // object

        function Person() {

        }

        function Colors() {

        }

        var person = new Person();
        var colors = new Colors();
        var pattern = /^9527$/gi;

        alert(person instanceof Person);
        alert(colors instanceof Colors);
        alert(pattern instanceof RegExp);
    }
}